package com.zte.market.service.impl;

import com.zte.login.pojo.Users;
import com.zte.login.service.UserService;
import com.zte.market.mapper.CustomMapper;
import com.zte.market.pojo.City;
import com.zte.market.pojo.Custom;
import com.zte.market.pojo.Province;
import com.zte.market.service.CityService;
import com.zte.market.service.CustomService;
import com.zte.market.service.ProvinceService;
import com.zte.util.PageModel;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: 许月
 * @Date: 2022/10/02/13:55
 */
@Service
public class CustomServiceImpl implements CustomService {
    @Autowired
    private CustomMapper customMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private ProvinceService provinceService;

    @Autowired
    private CityService cityService;

    /**
     * 获取全部的省
     * @return list
     */
    @Override
    public List<Province> getAllProvince() {
        return provinceService.queryProvinces();
    }

    /**
     * 用于订单页面的增加订单时显示客户名字列表
     * @param serviceid
     * @return
     */
    @Override
    public List<Custom> getAllByServiceId(Integer serviceid) {
        List<Custom> customList = new ArrayList<>();
        customList = customMapper.queryAllByServiceId(serviceid);
        return customList;
    }

    /**
     * 通过模糊查询 客户姓名 获取对应客户的 CustomId列表
     * @param customname
     * @return
     */
    @Override
    public List<Integer> queryCustomIdListByCustomname(String customname) {
        List<Integer> customIdList = new ArrayList<>();
        if(customname != null && !"".equals(customname)){
            customIdList =customMapper.queryCustomIdListByCustomname(customname);
        }
        return customIdList;
    }

    /**
     * 获取所有客户信息,并向数据中添加相关类
     * @return public List<Custom>
     */
    @Override
    public List<Custom> getAllCustom() {
        List<Custom> customList = customMapper.queryAll();
        for (int i=0;i<customList.size();i++) {
            Integer createid =  customList.get(i).getCreateid();
            Integer serviceid =  customList.get(i).getServiceid();
            Integer address = customList.get(i).getAddress();
            //向客户信息中添加 公司所在的地址信息
            if(address != null){
                City city = cityService.queryById(address);
                customList.get(i).setCity(city);
            }
            //向客户信息中添加 创建人员信息
            if(createid != null){
                Users  createUsers = userService.getUserById(createid);
                customList.get(i).setCreateUsers(createUsers);
            }
            //向客户信息中添加 服务人员信息
            if(serviceid != null){
                Users  serviceUsers = userService.getUserById(serviceid);
                customList.get(i).setServiceUsers(serviceUsers);
            }
        }
        return customList;
    }

    /**
     * 添加客户
     * @param custom 客户对象
     * @return 是否添加成功
     */
    @Override
    public boolean addByCustom(Custom custom) {
        Integer num = customMapper.addByCustom(custom);
        if(num == 1){
            return true;
        }
        return false;
    }

    /**
     * 将数据进行分页
     * @param customList 查询总记录数
     * @param pageSize 页面尺寸
     * @param pageNum 当前页面号
     * @return
     */
    @Override
    public PageModel<Custom> queryByPage(List<Custom> customList,int pageSize,int pageNum) {

        PageModel<Custom> customPage = new PageModel<>();
        if(customList != null && customList.size()!=0){
            customPage.setPageNum(pageNum);//设置当前页
            customPage.setPageSize(pageSize);//设置当前页的尺寸
            //总页数
            int totalPage = customList.size()/pageSize;
            if(customList.size()%pageSize != 0){
                totalPage += 1;
            }
            customPage.setTotalPage(totalPage); //设置总页数
            customPage.setTotalRows(customList.size());//设置总记录数

            List<Custom> pageList = new ArrayList<>();
            int start = (pageNum-1)*pageSize;
            int end = pageNum*pageSize;//当页数据中最后一条数据对应的索引
            if(customList.size()<end){
                end = customList.size();
            }
            for(int i=start;i<end;i++){
                pageList.add(customList.get(i));
            }

            customPage.setData(pageList);//设置存储数据
        }else {
            customPage.setTotalPage(1);
            customPage.setPageNum(1);
            customPage.setPageSize(7);
            customPage.setTotalRows(0);
            customPage.setData(null);
        }

        return customPage;
    }
    /**
     * 通过前端数据进行查询
     * @param custom 客户对象
     * @param cityList 城市集合
     * @param isAllot 是否被分配
     * @param pageSize 页面此尺寸
     * @param pageNum 当前页
     * @return 客户列表
     */
    @Override
    public PageModel<Custom> queryByParam(Custom custom, List<City> cityList, Integer isAllot,Integer pageSize,Integer pageNum) {
        List<Integer> cityIds = new ArrayList<>();
        if(cityList != null){
            for (int i = 0; i < cityList.size(); i++) {
                cityIds.add(cityList.get(i).getId());
            }
        }
        //是否分配，表示有没有服务人员id，-1表示未选择是否分配，1表示已分配，0表示未分配
        List<Custom> customList = customMapper.getByParam(custom,cityIds,isAllot);
        for (int i=0;i<customList.size();i++) {
            Integer createid =  customList.get(i).getCreateid();
            Integer serviceid =  customList.get(i).getServiceid();
            Integer address = customList.get(i).getAddress();
            //向客户信息中添加 公司所在的地址信息
            if(address != null){
                City city = cityService.queryById(address);
                customList.get(i).setCity(city);
            }
            //向客户信息中添加 创建人员信息
            if(createid != null){
                Users  createUsers = userService.getUserById(createid);
                customList.get(i).setCreateUsers(createUsers);
            }
            //向客户信息中添加 服务人员信息
            if(serviceid != null){
                Users  serviceUsers = userService.getUserById(serviceid);
                customList.get(i).setServiceUsers(serviceUsers);
            }
        }
        PageModel<Custom> customPage = queryByPage(customList, pageSize, pageNum);
        return customPage;
    }

    /**
     * 通过客户id查询信息
     * @param customId 客户id
     * @return
     */
    @Override
    public Custom queryByCustomId(Integer customId) {
        Custom custom = null;
        if(customId != null){
            custom = customMapper.getByCustomId(customId);
            //向客户信息中添加 公司所在的地址信息
            if(custom.getAddress() != null){
                City city = cityService.queryById(custom.getAddress());
                custom.setCity(city);
            }
            //向客户信息中添加 创建人员信息
            if(custom.getCreateid() != null){
                Users  createUsers = userService.getUserById(custom.getCreateid());
                custom.setCreateUsers(createUsers);
            }
            //向客户信息中添加 服务人员信息
            if(custom.getServiceid() != null){
                Users  serviceUsers = userService.getUserById(custom.getServiceid());
                custom.setServiceUsers(serviceUsers);
            }
        }
        if(custom != null){
            return custom;
        }
        return null;
    }
    /**
     * 修改 客户的 服务人员 id
     * @param customId 客户id
     * @param serviceid 服务人员id
     * @param distractime 分配时间
     * @return
     */
    @Override
    public boolean updateServiceIdByCustomId(Integer customId, Integer serviceid, Date distractime) {
        if(serviceid == null || "".equals(serviceid)){
            serviceid=0;
        }
        Integer rows =  customMapper.updateServiceIdByCustiomId(customId,serviceid,distractime);
        if(rows == 1){
            return true;
        }
        return false;
    }
    /**
     * 修改客户
     * @param custom 客户信息
     * @return
     */
    @Override
    public boolean updateCustom(Custom custom) {
        Integer rows = customMapper.updateCustom(custom);
        if(rows == 1){
            return true;
        }
        return false;
    }
    /**
     * 客服人员查询 客户
     * @param custom 客户查询信息
     * @param cityList 城市信息
     * @param isAllot 是否分配 -1（未选择状态）
     * @param pageSize 页面尺寸
     * @param pageNum 当前页
     * @param uid 客服人员 id
     * @return
     */
    @Override
    public PageModel<Custom> queryByServiceId(Custom custom, List<City> cityList, Integer isAllot, Integer pageSize, Integer pageNum, Integer uid) {
        List<Integer> cityIds = new ArrayList<>();
        if(cityList != null){
            for (int i = 0; i < cityList.size(); i++) {
                cityIds.add(cityList.get(i).getId());
            }
        }
        //是否分配，表示有没有服务人员id，-1表示未选择是否分配，1表示已分配，0表示未分配
        List<Custom> customList = customMapper.getByParam(custom,cityIds,isAllot);
        List<Custom> customs = new ArrayList<>();
        for (int i=0;i<customList.size();i++) {
            Integer createid =  customList.get(i).getCreateid();
            Integer serviceid =  customList.get(i).getServiceid();
            Integer address = customList.get(i).getAddress();
            //向客户信息中添加 公司所在的地址信息
            if(address != null){
                City city = cityService.queryById(address);
                customList.get(i).setCity(city);
            }
            //向客户信息中添加 创建人员信息
            if(createid != null){
                Users  createUsers = userService.getUserById(createid);
                customList.get(i).setCreateUsers(createUsers);
            }
            //向客户信息中添加 服务人员信息
            if(serviceid != null){
                Users  serviceUsers = userService.getUserById(serviceid);
                customList.get(i).setServiceUsers(serviceUsers);
            }
            //筛选 登录用户的服务人员信息
            if(uid == customList.get(i).getServiceid()){
                customs.add(customList.get(i));
            }
        }
        PageModel<Custom> customPage = queryByPage(customs, pageSize, pageNum);

        return customPage;
    }


    /**
     * 通过全名查询客户 id
     * @param customname
     * @return
     */
    @Override
    public List<Integer> queryCustomIdListByCustomnameAjax(String customname) {
        List<Integer> customIdList = customMapper.queryCustomIdListByCustomnameAjax(customname);
        return customIdList;
    }
}
